//
//  OnlineUserDetailViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/15.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "OnlineUserDetailViewController.h"
#import "OnlineUserInfo.h"
#import "MobilephoneGroupTableViewCell.h"
#import "TelephoneGroupTableViewCell.h"


NSString *const OnlineUserDetailViewControllerDetailInfoParamKey = @"OnlineUserDetailViewControllerDetailInfoParamKey";

@interface OnlineUserDetailViewController ()<XUIGroupTableViewDelegate,XUIGroupTableViewDataSource,MobilephoneStaticTextGroupTableViewCellDelegate,MFMessageComposeViewControllerDelegate,ExpandDynamicCellDelegate>
{
    XMSQueueDictionary *_qdData;
    OnlineUserInfo *_onlineUserInfo;
    CGFloat _mine_backgroundHeight;
    
    XUIGroupTableView *_gtable;
}
@end

@implementation OnlineUserDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    
    _qdData = [XMSQueueDictionary new];
    _mine_backgroundHeight = 100 + 44;
    
    [self initData];
    [self initUI];
}

- (void)initUI
{
    _gtable = [[XUIGroupTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    _gtable.delegate = self;
    _gtable.dataSource = self;

    [self.view addSubview:_gtable];
}

-(void)initData
{
    _onlineUserInfo = self.param[OnlineUserDetailViewControllerDetailInfoParamKey];

    [_qdData addKey:@"移动电话" value:@"移动电话"];
    [_qdData addKey:@"职称" value:@"职称"];
    [_qdData addKey:@"部门" value:@"部门"];
    [_qdData addKey:@"Email" value:@"Email"];
    [_qdData addKey:@"QQ" value:@"QQ"];
}

#pragma mark --
#pragma mark -- groupTableView delegate

- (NSUInteger)numberOfGroupsInGroupTableView:(XUIGroupTableView *)groupTableView
{
    return 2;
}

- (NSUInteger)groupTableView:(XUIGroupTableView *)groupTableView numberOfRowsInGroup:(NSInteger)group
{
    if(group ==0)
        return 1;
    else if(group == 1)
        return _qdData.count;
    return 0;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForHeaderInGroup:(NSInteger)group
{
    if(group ==0)
        return 0;
    return 10;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section ==0)
        return _mine_backgroundHeight;
    else if(indexPath.section == 1)
        return 54;
    return 0;
}

- (XUIGroupTableViewCell *)groupTableView:(XUIGroupTableView *)groupTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [self groupTableView:groupTableView heightForRowAtIndexPath:indexPath];
    if(indexPath.section ==0)
    {
        XUIGroupTableViewCell *cell = [[XUIGroupTableViewCell alloc] initWithFrame:CGRectZero];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        CGFloat width = groupTableView.width - 30 - 30;
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 0,  width, 30)];
        nameLabel.font = [UIFont systemFontOfSize:30];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.text = _onlineUserInfo.userName;
        nameLabel.bottom = height - 30;
        nameLabel.textAlignment = NSTextAlignmentCenter;
        
        cell.backgroundColor = [UIColor colorWithHex:mcrmSAASUIShinColorTextStyle2196F3Color];
        
        [cell addSubview:nameLabel];
        
        return cell;
    }
    else if(indexPath.section == 1)
    {
        CGSize size = [NSString charSizeWithSystemFontSize:15];
        
        if(indexPath.row == 0)
        {
            MobilephoneStaticTextGroupTableViewCell *cell = [[MobilephoneStaticTextGroupTableViewCell alloc] initWithFrame:CGRectMake(10, 0, groupTableView.width, 54)];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.title = @"手机";
            cell.text = _onlineUserInfo.mobilePhone;
            cell.delegate = self;
            return cell;
        }
        else if(indexPath.row == 1)
        {
            StaticTextGroupTableViewCell * cell = [[StaticTextGroupTableViewCell alloc] initWithFrame:CGRectMake(10, 0, groupTableView.width, 54)];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.title = @"职称";
            cell.text = _onlineUserInfo.positionJob;
            cell.delegate = self;
            return cell;
        }
        else if(indexPath.row == 2)
        {
            StaticTextGroupTableViewCell *cell = [[StaticTextGroupTableViewCell alloc] initWithFrame:CGRectMake(10, 0, size.width * 4, 54)];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.title = @"部门";
            cell.text = _onlineUserInfo.departmentName;
            return cell;
        }
        else if(indexPath.row == 3)
        {
            StaticTextGroupTableViewCell *cell = [[StaticTextGroupTableViewCell alloc] initWithFrame:CGRectMake(10, 0, size.width * 4, 54)];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.title = @"Email";
            cell.text = _onlineUserInfo.email;
            return cell;
        }
        else if(indexPath.row == 4)
        {
            StaticTextGroupTableViewCell *cell = [[StaticTextGroupTableViewCell alloc] initWithFrame:CGRectMake(10, 0, size.width * 4, 54)];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.title = @"QQ";
            cell.text = _onlineUserInfo.qq;
            return cell;
        }
    }
    
    return nil;
}

#pragma mark --
#pragma mark -- refresh mobilephone
-(void)mobilephoneStaticTextGroupTableViewCell:(MobilephoneStaticTextGroupTableViewCell *)cell didClickSMSButton:(id)sender
{
    if(cell.text.length >0 && [MFMessageComposeViewController canSendText])
    {
        MFMessageComposeViewController *picker = [MFMessageComposeViewController new];
        picker.recipients = @[cell.text];
        picker.messageComposeDelegate = self;
        [UIViewController presentViewController:picker animated:YES completion:^{}];
    }
}

-(void)mobilephoneStaticTextGroupTableViewCell:(MobilephoneStaticTextGroupTableViewCell *)cell didClickPhoneButton:(id)sender
{
    if(cell.text.length >0)
        [XMSCallTelephone callTelephone:cell.text];
}

#pragma mark --
#pragma mark -- message delegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result)
    {
        case MessageComposeResultCancelled:
        {
            [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            break;
        }
        case MessageComposeResultSent:
        {
            [XUIAlertView alertViewWithTitle:@"提示" message:@"短信发送成功" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex) {
                [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            }];
            break;
        }
        case MessageComposeResultFailed:
        {
            [XUIAlertView alertViewWithTitle:@"提示" message:@"短信发送失败" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex) {
                [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            }];
            break;
        }
        default:
        {
            [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            break;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
